import redis
import json
from config.config import *


def send_redis_hset(k1, k2, v):
    if not redisPassword:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, decode_responses=False)
    else:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, password=redisPassword,
                                    decode_responses=False)
    r = redis.Redis(connection_pool=pool)
    r.hset(k1, k2, v)


def get_redis_hset(k1, k2):
    if not redisPassword:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, decode_responses=False)
    else:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, password=redisPassword,
                                    decode_responses=False)
    r = redis.Redis(connection_pool=pool)
    value = r.hget(k1, k2)
    if value:
        v = value.decode().replace("'", '"')
        data = json.loads(v)
        return data
    else:
        return None


def get_set(key):
    if not redisPassword:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, decode_responses=False)
    else:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, password=redisPassword,
                                    decode_responses=False)
    r = redis.Redis(connection_pool=pool)
    name = r.get(key)
    return int(name.decode())


def send_redis_set(k, v):
    if not redisPassword:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, decode_responses=False)
    else:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, password=redisPassword,
                                    decode_responses=False)
    r = redis.Redis(connection_pool=pool)
    r.set(k, v)


def delte_redis_set(k):
    if not redisPassword:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, decode_responses=False)
    else:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, password=redisPassword,
                                    decode_responses=False)
    r = redis.Redis(connection_pool=pool)
    r.delete(k)
